//
//  TFSearchBar.m
//  TestByFF
//
//  Created by hanxifeng on 16/7/1.
//  Copyright © 2016年 haixu. All rights reserved.
//

#import "FSearchBar.h"

@interface FSearchBar()<UISearchBarDelegate>

@end

@implementation FSearchBar

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1];
        CGFloat paddingX = 5;
        CGFloat searchHeight = 30;
        CGFloat paddingY = (frame.size.height - 30)/2;
        _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(paddingX, paddingY, frame.size.width-2*paddingX, searchHeight)];
        _searchBar.placeholder = @"请输入要搜索的小区";
        _searchBar.delegate = self;
        _searchBar.backgroundImage = [self convertImageFromColor:[UIColor clearColor]];
        [self addSubview:_searchBar];
    }
    return  self;
}

#pragma mark - delegate
#pragma mark search bar delegate
//搜索框获取焦点时候触发
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    [self showCancleBtn:YES withText:YES];
    //无文本内容的时候
    if(searchBar.text.length==0||[searchBar.text isEqualToString:@""]||[searchBar.text isKindOfClass:[NSNull class]])
    {
        if ([_delegate respondsToSelector:@selector(FSearchBarDidBeginEditing:)]) {
            [_delegate FSearchBarDidBeginEditing:self];
        }
    }
    //输入框中有文本内容
    else
    {
        if([_delegate respondsToSelector:@selector(FSearchBarDidChangeEditing:withTextString:)])
        {
            [_delegate FSearchBarDidChangeEditing:self withTextString:searchBar.text];
        }
    }
}
//实时更新输入 实时触发
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    if([_delegate respondsToSelector:@selector(FSearchBarDidChangeEditing:withTextString:)])
    {
        [_delegate FSearchBarDidChangeEditing:self withTextString:searchBar.text];
    }
}
//取消按钮 触发
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    [self showCancleBtn:NO withText:NO];
    if ([_delegate respondsToSelector:@selector(FSearchBarCancleEditing:)]) {
        [_delegate FSearchBarCancleEditing:self];
    }

}
//完成按钮 触发
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    if ([_delegate respondsToSelector:@selector(FSearchBarSearchBtnClick:)]) {
        [_delegate FSearchBarSearchBtnClick:self];
    }
}
//是否显示取消按钮
-(void)showCancleBtn:(BOOL)isShow withText:(BOOL)isWith{
    if (!isShow) {//隐藏取消按钮
        [_searchBar resignFirstResponder];
    }
    [_searchBar setShowsCancelButton:isShow animated:YES];
    if (!isWith) {//不带字
        _searchBar.text = nil;
    }
}
#pragma mark - action
#pragma mark local action
//将uicolor转成uiimage
-(UIImage *)convertImageFromColor:(UIColor *)color{
    
    //转成image之后的尺寸
    CGRect contextFrame = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(contextFrame.size);
    //获取上下文
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    //用颜色填充 然后给frame
    CGContextSetFillColorWithColor(contextRef, [color CGColor]);
    CGContextFillRect(contextRef, contextFrame);
    //从上下文中获取image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    //结束上下文
    UIGraphicsEndImageContext();
    
    return image;
}
@end
